package com.jsgt.greekfire.ui.gfc

import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivity
import com.fhx.core.utils.ToastUtils
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.OtcAdBean
import com.jsgt.greekfire.common.util.XLinearLayoutManager
import com.jsgt.greekfire.factory.CoinGFCModelFactory
import com.jsgt.greekfire.ui.gfc.adapter.GFCCoinAdapter
import com.jsgt.greekfire.viewmodel.CoinCurrencyViewModel
import com.scwang.smartrefresh.header.MaterialHeader
import kotlinx.android.synthetic.main.activity_coin.*
import org.jetbrains.anko.startActivity

class CoinActivity : BaseActivity() {
    var tvTitle: TextView? = null
    var carList: ArrayList<OtcAdBean.ListBean>? = null

    private var gfcNum: String? = null
    private var usdtNum: String? = null
    private var dealAmount: String? = null
    private var guid: String? = null
    private var page = 1

    private val gfccoinAdapter by lazy {
        GFCCoinAdapter(this!!, R.layout.item_gfc_coin_list)
    }
    private val coinCurrencyViewModel: CoinCurrencyViewModel by lazy {
        ViewModelProvider(
            this,
            CoinGFCModelFactory()
        ).get(CoinCurrencyViewModel::class.java)
    }


    override fun getLayoutId(): Int = R.layout.activity_coin
    override fun main() {
        carList = ArrayList()

        coinCurrencyViewModel.getCoinlistData()
        coinCurrencyViewModel.getotcAdData(2, 1, page)
        initData()
        tvTitle = findViewById(R.id.title_tv)
        tvTitle?.text = resources.getString(R.string.optional_area)

        rvCoin.layoutManager = XLinearLayoutManager(this)
        rvCoin.adapter = gfccoinAdapter


        refreshCoin.setRefreshHeader(MaterialHeader(this))
        refreshCoin.setOnRefreshListener {
            page = 1
            coinCurrencyViewModel.getotcAdData(2, 1, page)
        }
        refreshCoin.setOnLoadMoreListener {
            coinCurrencyViewModel.getotcAdData(2, 1, ++page)
        }
        gfccoinAdapter.addChildClickViewIds(R.id.tvBuyGFC)
        gfccoinAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (view.id == R.id.tvBuyGFC) {
                coinCurrencyViewModel.getotcAdStateData(gfccoinAdapter.data.get(position).guid!!.toString(), "1")
                var s = gfccoinAdapter.data.get(position).totalAmount!! - gfccoinAdapter.data.get(position).turnoverAmount!!
                dealAmount = gfccoinAdapter.data.get(position).dealAmount!!.toString();
                gfcNum = gfccoinAdapter.data.get(position).dealNum!!.toString();
                guid = gfccoinAdapter.data.get(position).guid!!.toString();
                usdtNum = s.toString()
            }
        }
        ivOrder.setOnClickListener { v ->
            goToActivity<CommissionActivity>()
        }
        ivAdd.setOnClickListener { v ->
            goToActivity<SaleEntrustActivity>()
        }


    }

    private fun initData() {
        coinCurrencyViewModel.coinlistData.observe(this, Observer {
            it.result?.forEach { it1 ->
            }
        })
        coinCurrencyViewModel.otcAdData.observe(this, Observer {
            if (page == 1) {
                carList!!.clear()
            }
            if (it.result?.list!!.size <= 0) {
                if (refreshCoin != null) {
                    refreshCoin.finishRefresh();
                    refreshCoin.finishLoadMore();//不传时间则立即停止刷新    传入false表示加载失败
                    refreshCoin.finishLoadMoreWithNoMoreData();//没有加载更多
                }
            } else {
                carList!!.addAll(it.result?.list!!)
            }
            if (carList!!.size <= 0) {
                gfccoinAdapter.setEmptyView(R.layout.item_empty);
            }
            gfccoinAdapter?.replaceData(carList!!)

            if (refreshCoin != null) {
                refreshCoin.finishRefresh();
                refreshCoin.finishLoadMore();//不传时间则立即停止刷新    传入false表示加载失败
            }
        })
        coinCurrencyViewModel.otcAdStateData.observe(this, Observer {
            startActivity<BuyGFCCoinActivity>(
                "gfc" to gfcNum,
                "usdt" to usdtNum,
                "dealAmount" to dealAmount,
                "guid" to guid
            )
        })
        coinCurrencyViewModel.errorLiveData.observe(this, Observer {
            ToastUtils.show(it.toString())
            if (refreshCoin != null) {
                refreshCoin.finishRefresh();
                refreshCoin.finishLoadMore();//不传时间则立即停止刷新    传入false表示加载失败
            }

        })


    }

    override fun onResume() {
        super.onResume()
        coinCurrencyViewModel.getCoinlistData()
        refreshCoin.autoRefresh()
    }
}